home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 23 code / Documentary Synchronicity ƒ / Source ƒ / Document.c next >
Encoding:
C/C++ Source or Header  |  1995-06-10  |  4.8 KB  |  218 lines  |  [TEXT/KAHL]

  1. /* 4567890123456789012345678901234567890123456789012345678901234567 */
  2. /*
  3.  * File:              Document.c
  4.  * Author:            Mark H. Linton
  5.  * Creation Date:     4/24/95
  6.  * Modification Date: 
  7.  * Description:       A sample procedural shell for a typical 
  8.  *                    Macintosh document.
  9.  */
  10. #include <Icons.h>
  11. #include <Errors.h>
  12.  
  13. #include "Document.h"
  14.  
  15. #include "Toolbox.h"
  16.  
  17. #include "DSDocumentList.h"
  18.  
  19. static OSErr DrawDocumentData(short aType, Ptr aValue, long aRefCon);
  20. static OSErr WriteDocumentData(short aType, Ptr aValue, long aRefCon);
  21. static OSErr RemoveDocumentData(short aType, Ptr aValue, long aRefCon);
  22.  
  23. #if STRICT_WINDOWS
  24. void DoContentClick(WindowRef aWindow, EventRecord *anEvent) {
  25. #else
  26. void DoContentClick(WindowPtr aWindow, EventRecord *anEvent) {
  27. #endif
  28.     GrafPtr theCurrentPort;
  29.     OSErr theError;
  30.     Rect theRect;
  31.     Point *thePoint;
  32.     
  33.     GetPort(&theCurrentPort);
  34. #if STRICT_WINDOWS
  35.     SetPortWindowPort(aWindow);
  36. #else
  37.     SetPort(aWindow);
  38. #endif
  39.     
  40.     GlobalToLocal(&anEvent->where);
  41.     thePoint = (Point *)NewPtrClear(sizeof(Point));
  42.     if (thePoint != nil) {
  43.         thePoint->h = anEvent->where.h;
  44.         thePoint->v = anEvent->where.v;
  45.         theError = DSAddDocumentData(aWindow, kIconFamily, 
  46.             (Ptr)thePoint);
  47.     } else {
  48.         theError = MemError();
  49.     }
  50.     if (theError == noErr) {
  51.         theError = DSWindowDirty(aWindow);
  52.     }
  53.     if (theError == noErr) {
  54.         theRect.left = anEvent->where.h - 16;
  55.         theRect.right = theRect.left + 32;
  56.         theRect.top = anEvent->where.v - 16;
  57.         theRect.bottom = theRect.top + 32;
  58.         InvalRect(&theRect);
  59.     }
  60.     
  61.     SetPort(theCurrentPort);
  62.     
  63.     if (theError != noErr) {
  64.         ReportError(theError);
  65.     }
  66. }
  67.  
  68. #if STRICT_WINDOWS
  69. void DoDrawDocument(WindowRef aWindow) {
  70. #else
  71. void DoDrawDocument(WindowPtr aWindow) {
  72. #endif
  73.     GrafPtr theCurrentPort;
  74.     OSErr theError;
  75.     RgnHandle theCurrentClip;
  76.     Rect theClipRect;
  77.  
  78.     GetPort(&theCurrentPort);
  79. #if STRICT_WINDOWS
  80.     SetPortWindowPort(aWindow);
  81. #else
  82.     SetPort(aWindow);
  83. #endif
  84.     
  85.     theCurrentClip = NewRgn();
  86.     GetClip(theCurrentClip);
  87. #if STRICT_WINDOWS
  88.     theClipRect = GetWindowPort(aWindow)->portRect;
  89. #else
  90.     theClipRect = aWindow->portRect;
  91. #endif
  92.     theClipRect.right -= 15;
  93.     theClipRect.bottom -= 15;
  94.     ClipRect(&theClipRect);
  95.  
  96.     theError = DSForEachDocumentDataDo(aWindow, DrawDocumentData, 0L);
  97.  
  98.     SetClip(theCurrentClip);
  99.     DisposeRgn(theCurrentClip);
  100.  
  101.     SetPort(theCurrentPort);
  102.     
  103.     if (theError != noErr) {
  104.         ReportError(theError);
  105.     }
  106. }
  107.  
  108. OSErr DrawDocumentData(short aType, Ptr aValue, long aRefCon) {
  109.     OSErr theError;
  110.     Rect theRect;
  111.     Point thePoint;
  112.  
  113.     switch (aType) {
  114.     case kIconFamily:
  115.         thePoint = *(Point *)aValue;
  116.         theRect.left = thePoint.h - 16;
  117.         theRect.right = theRect.left + 32;
  118.         theRect.top = thePoint.v - 16;
  119.         theRect.bottom = theRect.top + 32;
  120.         theError = PlotIconID(&theRect, atNone, ttNone, 131);
  121.         break;
  122.     default:
  123.         theError = paramErr;
  124.     }
  125.     return theError;
  126. }
  127.  
  128. #if STRICT_WINDOWS
  129. OSErr DoWriteDocument(WindowRef aWindow, short aDFRefNum) {
  130. #else
  131. OSErr DoWriteDocument(WindowPtr aWindow, short aDFRefNum) {
  132. #endif
  133.     OSErr theError;
  134.     short theDataCount;
  135.     long theSize;
  136.  
  137.     theError = DSCountDocumentData(aWindow, &theDataCount);
  138.     theError = SetFPos(aDFRefNum, fsFromStart, 0L);
  139.     theSize = sizeof(short);
  140.     theError = FSWrite(aDFRefNum, &theSize, &theDataCount);
  141.     theError = DSForEachDocumentDataDo(aWindow, WriteDocumentData, 
  142.         (long)aDFRefNum);
  143.     
  144.     return theError;
  145. }
  146.  
  147. OSErr WriteDocumentData(short aType, Ptr aValue, long aRefCon) {
  148.     OSErr theError;
  149.     Point thePoint;
  150.     long theSize;
  151.  
  152.     switch (aType) {
  153.     case kIconFamily:
  154.         thePoint = *(Point *)aValue;
  155.         theSize = sizeof(Point);
  156.         theError = FSWrite((short)aRefCon, &theSize, &thePoint);
  157.         break;
  158.     default:
  159.         theError = paramErr;
  160.     }
  161.     return theError;
  162. }
  163.  
  164. #if STRICT_WINDOWS
  165. OSErr DoReadDocument(WindowRef aWindow) {
  166. #else
  167. OSErr DoReadDocument(WindowPtr aWindow) {
  168. #endif
  169.     OSErr theError;
  170.     short theDataCount, theItem;
  171.     short theFRefNum;
  172.     long theSize;
  173.     Point *thePoint;
  174.  
  175.     theError = DSGetWindowDFRefNum(aWindow, &theFRefNum);
  176.     theError = SetFPos(theFRefNum, fsFromStart, 0L);
  177.     theSize = sizeof(short);
  178.     theError = FSRead(theFRefNum, &theSize, &theDataCount);
  179.     theSize = sizeof(Point);
  180.     for (theItem = 0; theItem < theDataCount; theItem++) {
  181.         thePoint = (Point *)NewPtrClear(sizeof(Point));
  182.         if (thePoint != nil) {
  183.             theError = FSRead(theFRefNum, &theSize, thePoint);
  184.             theError = DSAddDocumentData(aWindow, kIconFamily, 
  185.                 (Ptr)thePoint);
  186.         }
  187.     }
  188.     
  189.     return theError;
  190. }
  191.  
  192. #if STRICT_WINDOWS
  193. OSErr DoRemoveDocumentData(WindowRef aWindow) {
  194. #else
  195. OSErr DoRemoveDocumentData(WindowPtr aWindow) {
  196. #endif
  197.     OSErr theError;
  198.     
  199.     theError = DSForEachDocumentDataDo(aWindow, RemoveDocumentData, 
  200.         0L);
  201.     return theError;
  202. }
  203.  
  204. OSErr RemoveDocumentData(short aType, Ptr aValue, long aRefCon) {
  205.     OSErr theError;
  206.  
  207.     switch (aType) {
  208.     case kIconFamily:
  209.         DisposePtr(aValue);
  210.         theError = MemError();
  211.         break;
  212.     default:
  213.         theError = paramErr;
  214.     }
  215.     return theError;
  216. }
  217.  
  218.